home *** CD-ROM | disk | FTP | other *** search
- /* HARDERR.C --- p. 620 */
- #include <stdio.h>
- #include <dos.h>
- /* Prototype of our critical error handler */
- int harderror_handler(int, int, int, int);
- unsigned error_flag = 0;
- main()
- {
- unsigned drivea = 1;
- unsigned long total_space, free_space, bytes_per_cluster;
- struct dfree dfinfo;
- /* Install our critical error handler */
- harderr(harderror_handler);
- printf("We will check our critical error handler.\n"
- "Make sure drive A: is empty. Hit any key to continue: ");
- getch();
- /* Try an operation on drive A: */
- getdfree(drivea, &dfinfo);
- /* If error-flag is set, repeat call to getdfree */
- while(error_flag)
- {
- error_flag = 0;
- getdfree(drivea, &dfinfo);
- }
- /* Compute space statistics and display result */
- bytes_per_cluster = dfinfo.df_sclus * dfinfo.df_bsec;
- total_space = dfinfo.df_total * bytes_per_cluster;
- free_space = dfinfo.df_avail * bytes_per_cluster;
- printf("\n%ld bytes free out of %ld bytes of total space.\n",
- free_space, total_space);
- }
- /*-------------------------------------*/
- /* Critical error handler */
- #define DRIVE_NOT_READY 2
- #define HARDERR_ABORT 2
- int harderror_handler(int errorcode, int deverror, int bpval, int sival)
- {
- unsigned far *devhdr;
- char dletter;
- /* Set up pointer to device header */
- devhdr = MK_FP((unsigned)bpval, (unsigned)sival);
- /* Set a flag to let our program know about the error */
- error_flag = 1;
- /* Check if this is a "drive not ready" error */
- if((errorcode & 0xff) == DRIVE_NOT_READY)
- {
- /* Find out which drive, it's in low byte of deverror */
- dletter = 'A' + (deverror & 0xff);
- /* Ask user to insert a diskette into the drive */
- printf("\nDrive %c in not ready.\n"
- "Please insert a diskette and hit any key to continue: ", dletter);
- getch(); /* Read key before returning */
- /* Use hardretn to go back to your program */
- hardretn(-1);
- }
- else
- {
- /* Unkown error, print message and abort program */
- printf("Unknown critical error. Aborting...\n");
- hardresume(HARDERR_ABORT);
- }
- }